{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Longest Subarray With Maximum Bitwise AND"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #brainteaser #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #脑筋急转弯 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: longestSubarray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #按位与最大的最长子数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> 。</p>\n",
    "\n",
    "<p>考虑 <code>nums</code> 中进行 <strong>按位与（bitwise AND）</strong>运算得到的值 <strong>最大</strong> 的 <strong>非空</strong> 子数组。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>换句话说，令 <code>k</code> 是 <code>nums</code> <strong>任意</strong> 子数组执行按位与运算所能得到的最大值。那么，只需要考虑那些执行一次按位与运算后等于 <code>k</code> 的子数组。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回满足要求的 <strong>最长</strong> 子数组的长度。</p>\n",
    "\n",
    "<p>数组的按位与就是对数组中的所有数字进行按位与运算。</p>\n",
    "\n",
    "<p><strong>子数组</strong> 是数组中的一个连续元素序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,3,2,2]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\n",
    "子数组按位与运算的最大值是 3 。\n",
    "能得到此结果的最长子数组是 [3,3]，所以返回 2 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>\n",
    "子数组按位与运算的最大值是 4 。 \n",
    "能得到此结果的最长子数组是 [4]，所以返回 1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [longest-subarray-with-maximum-bitwise-and](https://leetcode.cn/problems/longest-subarray-with-maximum-bitwise-and/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [longest-subarray-with-maximum-bitwise-and](https://leetcode.cn/problems/longest-subarray-with-maximum-bitwise-and/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,3,2,2]', '[1,2,3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        left = 0\n",
    "        zeros = 0\n",
    "        ans = 0\n",
    "\n",
    "        for right in range(n):\n",
    "            if nums[right] == 0:\n",
    "                zeros += 1\n",
    "\n",
    "            while zeros > 1:\n",
    "                if nums[left] == 0:\n",
    "                    zeros -= 1\n",
    "                left += 1\n",
    "\n",
    "            ans = max(ans, right - left + 1 - zeros)\n",
    "\n",
    "        return ans - 1 if ans == n else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        length1 = 0 #记录删除下次遇到的第一个0后的1的长度\n",
    "        length2 = 0 #记录下次遇到的第一个0后面的1的长度\n",
    "        count = 0\n",
    "        max_length = 0  #记录全为1的子数组的最长长度\n",
    "        for i in nums:\n",
    "            if i == 0:\n",
    "                length1 = length2\n",
    "                length2 = 0\n",
    "                count += 1\n",
    "                continue\n",
    "            length1 += 1\n",
    "            length2 += 1\n",
    "            if length1 > max_length:\n",
    "                max_length = length1\n",
    "        if count == 0:\n",
    "            max_length -= 1\n",
    "        return max_length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        tmp = []\n",
    "        t = 0\n",
    "        for i in nums:\n",
    "            if i==1:\n",
    "                if t>=0:\n",
    "                    t+=1\n",
    "                else:\n",
    "                    tmp.append(t)\n",
    "                    t = 1\n",
    "            else:\n",
    "                if t<=0:\n",
    "                    t-=1\n",
    "                else:\n",
    "                    tmp.append(t)\n",
    "                    t = -1\n",
    "        tmp.append(t)\n",
    "        res = max(tmp)\n",
    "        if len(tmp)==1:\n",
    "            if tmp[0]>0:\n",
    "                return tmp[0]-1\n",
    "            return 0\n",
    "        for i in range(len(tmp)):\n",
    "            if tmp[i]==-1 and i-1>=0 and i+1<len(tmp):\n",
    "                res = max(res,tmp[i-1]+tmp[i+1])\n",
    "        return res\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        #\n",
    "        res = [0]\n",
    "        for i in nums:\n",
    "            if i == 0:\n",
    "                res.append(i)\n",
    "            elif i == 1 and res[-1] != 0:\n",
    "                res[-1] += 1\n",
    "            elif i == 1 and res[-1] == 0:\n",
    "                res.append(i)\n",
    "        # print(res)\n",
    "        res = res + [0]\n",
    "        ans = 0\n",
    "        if len(res) == 3:\n",
    "            return max(res[1]-1, 0)\n",
    "        for i in range(1, len(res)-1):\n",
    "            if res[i] == 0:\n",
    "                ans = max(ans, res[i-1]+res[i+1])\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        indexNot1 = []\n",
    "        for i ,e in enumerate(nums):\n",
    "            if e != 1:\n",
    "                indexNot1.append(i)\n",
    "        m , n = len(nums) , len(indexNot1)\n",
    "        if n < 2:\n",
    "            return len(nums) - 1\n",
    "        indexNot1.append(m)\n",
    "        res = indexNot1[1] - 1\n",
    "        for i in range(n - 1):\n",
    "            res = max(res , indexNot1[i + 2] - indexNot1[i] - 2)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        l = r = cnt = ans = 0\n",
    "        while r < len(nums):\n",
    "            if nums[r] == 0:\n",
    "                cnt += 1\n",
    "            r += 1\n",
    "            while cnt > 1:\n",
    "                if nums[l] == 0:\n",
    "                    cnt -= 1\n",
    "                l += 1\n",
    "            ans = max(ans, r - l - 1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "\n",
    "        n = len(nums)\n",
    "        pre = -1\n",
    "        ans = cnt = l = r = 0\n",
    "        while r < n:\n",
    "            if nums[r] == 1:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                pre = cnt\n",
    "                cnt = 0\n",
    "            r += 1  \n",
    "            ans = max(ans, pre+cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums) -> int:\n",
    "        left, right = 0, 0\n",
    "        zero_count = 0\n",
    "        ans = 0\n",
    "        while right < len(nums):\n",
    "            right_enum = nums[right]\n",
    "            if right_enum == 0:\n",
    "                zero_count += 1\n",
    "            while zero_count > 1:\n",
    "                left_enum = nums[left]\n",
    "                if left_enum == 0:\n",
    "                    zero_count -= 1\n",
    "                left += 1\n",
    "            ans = max(ans, right - left)\n",
    "            right += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        slow, fast = 0, 0\n",
    "        maxLength = 0\n",
    "        while fast < len(nums):\n",
    "            if nums[fast] == 1:\n",
    "                fast += 1\n",
    "            elif nums[fast] == 0 and 0 in nums[slow:fast]:\n",
    "                maxLength = max(maxLength, fast-slow-1)\n",
    "                slow += 1\n",
    "            elif nums[fast] == 0 and 0 not in nums[slow:fast]:\n",
    "                fast += 1\n",
    "        else:\n",
    "            maxLength = max(maxLength, fast-slow-1)\n",
    "        \n",
    "        return maxLength"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        left, right = 0, 0\n",
    "        cnt_zeros = 0 \n",
    "        while right < n:\n",
    "            if nums[right] == 0:\n",
    "                cnt_zeros += 1\n",
    "            while cnt_zeros > 1:\n",
    "                if nums[left] == 0:\n",
    "                    cnt_zeros -= 1\n",
    "                left += 1\n",
    "                if cnt_zeros == 0:\n",
    "                    res = len(nums) - 1\n",
    "            res = max(res, right - left)\n",
    "            right += 1\n",
    "        return res\n",
    "\n",
    "#         length=len(nums)\n",
    "#         right=0\n",
    "#         left=0\n",
    "#         max_count=0\n",
    "#         count=0\n",
    "#         for right in range(length):\n",
    "#             if nums[right]==0:\n",
    "#                 count+=1\n",
    "#             while count>1:\n",
    "#                 if(nums[left]==1):\n",
    "#                     count-=1\n",
    "#                 if count==0:\n",
    "#                     max_count=length-1\n",
    "#             max_count=max(max_count,right-left)\n",
    "#             right+=1\n",
    "#         return max_count\n",
    "# class Solution:\n",
    "#     def longestsubarray(self, nums: list[int]) -> int:\n",
    "#         n = len(nums)\n",
    "#         res = 0\n",
    "#         left, right = 0, 0\n",
    "#         cnt_zeros = 0 \n",
    "#         while right < n:\n",
    "#             if nums[right] == 0:\n",
    "#                 cnt_zeros += 1\n",
    "#             while cnt_zeros > 1:\n",
    "#                 if nums[left] == 0:\n",
    "#                     cnt_zeros -= 1\n",
    "#                 left += 1\n",
    "#                 if cnt_zeros == 0:\n",
    "#                     res = len(nums) - 1\n",
    "#             res = max(res, right - left)\n",
    "#             right += 1\n",
    "#         return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "\n",
    "        arr = [nums[0]]\n",
    "        for v in nums[1:]:\n",
    "            if v and arr[-1]:\n",
    "                arr[-1] += 1\n",
    "            else:\n",
    "                arr.append(v)\n",
    "        \n",
    "        if len(arr)==1:\n",
    "            if arr[0]:\n",
    "                return arr[0]-1\n",
    "            else:\n",
    "                return 0\n",
    "        \n",
    "        ans = arr[1]\n",
    "        arr.append(0)\n",
    "        for i in range(1, len(arr)-1):\n",
    "            ans = max(ans, arr[i-1]+arr[i+1])\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        left, right = 0, 0\n",
    "        ans, cnt = 0, 0\n",
    "        while right < len(nums):\n",
    "            if nums[right] == 0:\n",
    "                cnt += 1\n",
    "            while left < len(nums) and cnt > 1:\n",
    "                if nums[left] == 0:\n",
    "                    cnt -= 1\n",
    "                left += 1\n",
    "            ans = max(ans, right - left)\n",
    "            right += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        f1 = f2 = ret = 0\n",
    "        for v in nums:\n",
    "            if v == 0:\n",
    "                f1 = f2\n",
    "                f2 = 0\n",
    "            else:\n",
    "                f1 += 1\n",
    "                f2 += 1\n",
    "            ret = max(ret, f1)\n",
    "        return ret - 1 if ret == len(nums) else ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        # 双指针？\n",
    "        l = 0\n",
    "        right = 0\n",
    "        count0 = 0\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        while right < n:\n",
    "            if nums[right] == 0:\n",
    "                count0 += 1\n",
    "            while count0 > 1:\n",
    "                if nums[l] == 0:\n",
    "                    count0 -= 1\n",
    "                l += 1\n",
    "            res = max(res, right - l)\n",
    "            right += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        subsum = 0\n",
    "        l, r, ans = 0, 0, -1\n",
    "        for r in range(len(nums)):\n",
    "            subsum += nums[r]\n",
    "            if r - l + 1 - subsum > 1 and r - l > ans:\n",
    "                ans = r - l\n",
    "                print(l, r)\n",
    "            while r - l > subsum:\n",
    "                subsum -= nums[l]\n",
    "                l += 1\n",
    "        ans = max(ans, r - l + 1)\n",
    "        return ans - 1\n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        last = 0\n",
    "        temp = 0\n",
    "        ans = 0\n",
    "        nums.append(0)\n",
    "        for num in nums:\n",
    "            if num ==0:\n",
    "                ans = max(ans,temp+last)\n",
    "                last = temp\n",
    "                temp = 0\n",
    "            else:\n",
    "                temp += 1\n",
    "        \n",
    "        return ans if ans != len(nums)-1 else len(nums)-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        left, max_len, zero_cout = 0, 0, 0\n",
    "\n",
    "        for right in range(len(nums)):\n",
    "            if nums[right] == 0:\n",
    "                zero_cout += 1\n",
    "\n",
    "            while zero_cout > 1:\n",
    "                if nums[left] == 0:\n",
    "                    zero_cout -= 1\n",
    "                left += 1\n",
    "            \n",
    "            max_len = max(max_len, right - left)\n",
    "        \n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "\n",
    "        nums_dict = {1:0, 0:0}\n",
    "\n",
    "        max_one = 0\n",
    "\n",
    "        left,right = 0,0\n",
    "\n",
    "        # 如果nums列表里的0小于等于1个那么直接返回长度减一即可\n",
    "        if nums.count(0) <= 1:\n",
    "            return len(nums)-1\n",
    "\n",
    "        while right < len(nums):\n",
    "\n",
    "            key = nums[right]\n",
    "            nums_dict[key]+=1\n",
    "\n",
    "            while nums_dict[0] > 1:\n",
    "                max_one = max(max_one,(right-left-1))\n",
    "                nums_dict[nums[left]]-=1\n",
    "                left+=1\n",
    "            right+=1\n",
    "\n",
    "        max_one = max(max_one, (right-left-1))\n",
    "\n",
    "        return max_one\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "\n",
    "        res = ''\n",
    "        for i in nums:\n",
    "            res = res + str(i)\n",
    "        ss = res.split(\"0\")\n",
    "        print(ss)\n",
    "        if(len(ss) == 1):\n",
    "            return len(nums) - 1\n",
    "        \n",
    "        if(len(ss) == 0):\n",
    "            return 0 \n",
    "        \n",
    "        maxx = -1\n",
    "        for i in range(len(ss)-1):\n",
    "            print(len(ss[i]) + len(ss[i+1]))\n",
    "            d = len(ss[i]) + len(ss[i+1])\n",
    "            if( d > maxx):\n",
    "                maxx = d \n",
    "        \n",
    "        return maxx \n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = []\n",
    "        nums.append(0)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                a += 1\n",
    "            elif i == len(nums)-1 and a == i:\n",
    "                return a-1 \n",
    "            else:\n",
    "                b.append(a)\n",
    "                a = 0 \n",
    "        clo = [b[i]+b[i+1] for i in range(len(b)-1)] \n",
    "        #clo只有一个元素的时候无法用max\n",
    "        #法一，增加一个非正数\n",
    "        #clo.append(0)\n",
    "        \n",
    "        #return max(clo)\n",
    "        #法二，选定错误类型\n",
    "        return max(clo,default = 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = []\n",
    "        nums.append(0)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                a += 1\n",
    "            elif i == len(nums)-1 and a == i:\n",
    "                return a-1 \n",
    "            else:\n",
    "                b.append(a)\n",
    "                a = 0 \n",
    "        clo = [b[i]+b[i+1] for i in range(len(b)-1)] \n",
    "        clo.append(0)\n",
    "        \n",
    "        return max(clo)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        left_one = [0] * len(nums)\n",
    "         \n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i - 1] == 1:\n",
    "                left_one[i] = left_one[i - 1] + 1  \n",
    "        if left_one[-1] == len(nums) - 1:\n",
    "            return left_one[-1]\n",
    "\n",
    "        cur_ones = 0\n",
    "        max_count = 0\n",
    "        for i in reversed(range(len(nums))):\n",
    "            if nums[i] == 0:\n",
    "                max_count = max(max_count, left_one[i] + cur_ones)\n",
    "                cur_ones = 0\n",
    "            else:\n",
    "                cur_ones += 1 \n",
    "        \n",
    "        return max_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        presum = [0] *(n+1)\n",
    "        for i in range(n):\n",
    "            presum[i+1] = presum[i] + nums[i]\n",
    "        i = 0\n",
    "        big = 0\n",
    "        for j in range(n):\n",
    "            while j-i+1-(presum[j+1]-presum[i])>1:\n",
    "                i+=1\n",
    "            big = max(big, j-i+1)\n",
    "        return big-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        p = [0] + nums\n",
    "        for i in range(1, len(p)):\n",
    "            p[i] += p[i-1]\n",
    "        res = 0\n",
    "        left, right = 0, 0\n",
    "        while right < len(nums):\n",
    "            right += 1\n",
    "            while right - left > p[right] - p[left] + 1:\n",
    "                left += 1\n",
    "            res = max(res, right - left - 1)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        zeros=[-1]\n",
    "        i,n=0,len(nums)\n",
    "        ans=0\n",
    "        while i<n:\n",
    "            if nums[i]==0:\n",
    "                zeros.append(i)\n",
    "            i+=1\n",
    "        zeros.append(n)\n",
    "        z=len(zeros)\n",
    "        if z==2:\n",
    "            return n-1\n",
    "        i=0\n",
    "        while i<z-2:\n",
    "            ans=max(ans,zeros[i+2]-zeros[i]-2)\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        zeros = [-1]\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0:\n",
    "                zeros.append(i)\n",
    "        zeros.append(len(nums))\n",
    "        res = -1\n",
    "        for i in range(1, len(zeros)-1):\n",
    "            one_length = zeros[i+1] - zeros[i-1] - 2\n",
    "            res = max(res, one_length)\n",
    "            \n",
    "        return res if res != -1 else len(nums) - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        total = sum(nums)\n",
    "        ans = 0\n",
    "        if total >= n - 1:\n",
    "            return n - 1\n",
    "\n",
    "        pre = [0]\n",
    "        for num in nums:\n",
    "            pre.append(pre[-1] + (1 - num))\n",
    "\n",
    "        left = 0\n",
    "        while left < n:\n",
    "            right = bisect.bisect_right(pre, pre[left] + 1)\n",
    "            ans = max(ans, right - left - 2)\n",
    "            left = bisect.bisect_left(pre, pre[left] + 1)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        if 0 not in nums:\n",
    "            return len(nums) - 1\n",
    "        idx0 = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 0:\n",
    "                idx0.append(i)\n",
    "        if len(idx0) == 1:\n",
    "            return len(nums) - 1\n",
    "        idx0.append(len(nums))\n",
    "        max_count = count = idx0[1] - 1\n",
    "        for i in range(len(idx0) - 2):\n",
    "            count = idx0[i + 2] - idx0[i] - 2\n",
    "            max_count = max(max_count, count)\n",
    "        return max_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        lsum = [0]\n",
    "        for i in range(len(nums)):\n",
    "            lsum.append(lsum[-1] + nums[i])\n",
    "        # lsum = lsum[1:]\n",
    "        print(lsum)\n",
    "\n",
    "        s, f= 0,1\n",
    "        ans=0\n",
    "        while f < len(lsum):\n",
    "\n",
    "            if lsum[f] - lsum[s] == f-s or lsum[f] - lsum[s] == f-s-1:\n",
    "                # if lsum[f] - lsum[s] == f-s:\n",
    "                    # curlen = f-s-1\n",
    "                # if lsum[f] - lsum[s] == f-s-1:\n",
    "                curlen = f-s-1\n",
    "                ans = max(ans, curlen)\n",
    "                print(ans, s, f)\n",
    "                f += 1\n",
    "            else:\n",
    "                s += 1\n",
    "            \n",
    "            print(s, f)\n",
    "               \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        zeroList = []\n",
    "        for i in range(n):\n",
    "            if nums[i] == 0:\n",
    "                zeroList.append(i)\n",
    "        if not zeroList:\n",
    "            return n - 1\n",
    "        \n",
    "        zeroList.insert(0,-1)\n",
    "        zeroList.append(len(nums))\n",
    "        print(zeroList)\n",
    "        for i in range(len(zeroList)-2):\n",
    "            res = max(res, zeroList[i+2] - zeroList[i] - 2)\n",
    "        return res    \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        pre = [nums[0]]\n",
    "        for i in range(1, n):\n",
    "            pre.append(pre[-1] + 1 if nums[i] else 0)\n",
    "        \n",
    "        suf = [nums[n-1]] \n",
    "        for i in range(0, n-1)[::-1]:\n",
    "            suf.insert(0, suf[0] + 1 if nums[i] else 0)\n",
    "        \n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            presum =  0 if i== 0 else  pre[i-1]\n",
    "            sufsum =  0 if i==n-1 else suf[i+1]\n",
    "            ans = max(ans, presum + sufsum)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        mn = max(nums)\n",
    "        cnt = 0\n",
    "        for x in nums:\n",
    "            if mn == x:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        retVal = 0\n",
    "\n",
    "        # The maximum possible bitwise AND should not be less than a largest element.\n",
    "        # Therefore, the subarray must only include one or more largest elements.\n",
    "        maxNum = max(nums)\n",
    "        count = 0\n",
    "        for num in nums:\n",
    "            if num == maxNum:\n",
    "                count += 1\n",
    "            else:\n",
    "                count = 0\n",
    "            retVal = max(retVal, count)\n",
    "\n",
    "        return retVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        #copy 0x3\n",
    "        mx = max(nums)\n",
    "        ans = cnt =0\n",
    "        for x in nums:\n",
    "            if x ==mx:\n",
    "                cnt +=1\n",
    "                ans = max(ans,cnt)\n",
    "            else:\n",
    "                cnt =0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        val = max(nums)\n",
    "        res = 0\n",
    "        cnt = 0\n",
    "        for x in nums:\n",
    "            if x == val:\n",
    "                cnt += 1\n",
    "                res = max(res, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        max_value = max(nums)\n",
    "        cnt = 0\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i == max_value:\n",
    "                cnt += 1\n",
    "                ans = max(cnt, ans)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\r\n",
    "        #求最大值连续子数组长度的最大长度\r\n",
    "        maxx = max(nums)\r\n",
    "        cnt = 0\r\n",
    "        ans = 0\r\n",
    "        for num in nums:\r\n",
    "            if num == maxx:\r\n",
    "                cnt += 1\r\n",
    "                ans = max(cnt, ans)\r\n",
    "            else:\r\n",
    "                cnt = 0\r\n",
    "        return ans\r\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        target=max(nums)\n",
    "        ans=1\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i<n-ans:\n",
    "            if nums[i+ans]<target:\n",
    "                i+=ans+1\n",
    "                continue\n",
    "            if nums[i]==target:\n",
    "                temp=0\n",
    "                while i<n and nums[i]==target:\n",
    "                    i+=1\n",
    "                    temp+=1\n",
    "                ans=max(ans,temp)\n",
    "            else:\n",
    "                i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        maxn = 0\n",
    "        t = 0\n",
    "        big = max(nums)\n",
    "        for num in nums:\n",
    "            if num==big:\n",
    "                t+=1\n",
    "                maxn = max(maxn, t)\n",
    "            else:\n",
    "                t = 0\n",
    "        return maxn\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        mx = cnt = 0\n",
    "        for n in nums:\n",
    "            if n == mx:\n",
    "                cnt += 1\n",
    "            elif n > mx:\n",
    "                mx = n\n",
    "                ans = cnt = 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "            if cnt > ans:\n",
    "                ans = cnt\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        # 找最大值\n",
    "        mx = max(nums)\n",
    "        res = 1\n",
    "        cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "            res = max(res, cnt)\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        curMax=0\n",
    "        ans=0\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i<n:\n",
    "            if i+ans<n and nums[i+ans]<curMax:\n",
    "                i+=ans+1\n",
    "                continue\n",
    "            if nums[i]>=curMax:\n",
    "                if nums[i]>curMax:\n",
    "                    ans=0\n",
    "                    curMax=nums[i]\n",
    "                j=i\n",
    "                while i<n and nums[i]==curMax:\n",
    "                    i+=1\n",
    "                ans=max(ans,i-j)\n",
    "            else:\n",
    "                i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        m = max(nums)\n",
    "        ans = 0\n",
    "        tmp = 0\n",
    "        for i in nums:\n",
    "            if i == m:\n",
    "                tmp += 1\n",
    "            else:\n",
    "                tmp = 0\n",
    "            ans = max(ans, tmp)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        target=max(nums)\n",
    "        ans=1\n",
    "        last=-1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==target:\n",
    "                ans=max(ans,i-last)\n",
    "            else:\n",
    "                last=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def longestSubarray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        a = max(nums)\n",
    "        n = len(nums)\n",
    "        count = 1\n",
    "        res = 1\n",
    "        for i in range(1, n):\n",
    "            if nums[i] == nums[i-1] == a:\n",
    "                count += 1\n",
    "                res = max(res, count)\n",
    "            else: count = 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        maxlen = 0\n",
    "        maxvalue = max(nums)\n",
    "        cnt = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==maxvalue:\n",
    "                cnt += 1\n",
    "                maxlen = max(maxlen,cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return maxlen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def longestSubarray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        a = max(nums)\n",
    "        n = len(nums)\n",
    "        count = 1\n",
    "        res = 1\n",
    "        for i in range(1, n):\n",
    "            if nums[i] == nums[i-1] == a:\n",
    "                count += 1\n",
    "                res = max(res, count)\n",
    "            else: count = 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        target=max(nums)\n",
    "        ans=0\n",
    "        n=len(nums)\n",
    "        count=0\n",
    "        i=0\n",
    "        while i<n:\n",
    "            if nums[i]==target:\n",
    "                count+=1\n",
    "            else:\n",
    "                ans=max(ans,count)\n",
    "                count=0\n",
    "                if i+ans>=n:break\n",
    "                if nums[i+ans]<target:i+=ans\n",
    "            i+=1\n",
    "        ans=max(ans,count)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        max_ = max(nums)\n",
    "\n",
    "        i = 0\n",
    "        ans = 1\n",
    "        while i < len(nums):\n",
    "            while i < len(nums) and nums[i] != max_:\n",
    "                i += 1\n",
    "            \n",
    "            if i == len(nums):\n",
    "                break\n",
    "\n",
    "            start = i\n",
    "\n",
    "            while i < len(nums) and nums[i] == max_:\n",
    "                i += 1\n",
    "            \n",
    "            ans = max(ans, i - start)\n",
    "\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        val = max(nums)\n",
    "        max_len = 0\n",
    "        curr_len = 0\n",
    "        for num in nums:\n",
    "            if num == val:\n",
    "                curr_len += 1\n",
    "                max_len = max(max_len, curr_len)\n",
    "            else:\n",
    "                curr_len = 0\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        t = max(nums)\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            if nums[i] != t:\n",
    "                i += 1\n",
    "                continue\n",
    "            j = i\n",
    "            while j < n and nums[j] == t: j += 1\n",
    "            ans = max(ans,j-i)\n",
    "            i = j\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = cnt = mx = 0\n",
    "        for x in nums:\n",
    "            if x > mx:\n",
    "                mx = x\n",
    "                ans = cnt = 1\n",
    "            elif x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        maxnum = max(nums)\n",
    "        n = len(nums)\n",
    "        result = 1\n",
    "        left, right = 0, 0\n",
    "        while(left < n):\n",
    "            if(nums[left] == maxnum):\n",
    "                right = left\n",
    "                while(right < n and nums[right] == maxnum):\n",
    "                    right += 1\n",
    "                result = max(result, right - left)\n",
    "                left = right \n",
    "            else:\n",
    "                left += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = 1\n",
    "        n = len(nums)\n",
    "        cur = 0\n",
    "        for i in range(n):\n",
    "            if nums[i] == mx:\n",
    "                cur += 1\n",
    "                ans = max(ans, cur)\n",
    "            else:\n",
    "                cur = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def longestSubarray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        a = max(nums)\n",
    "        n = len(nums)\n",
    "        count = 1\n",
    "        res = 1\n",
    "        for i in range(1, n):\n",
    "            if nums[i] == nums[i-1] == a:\n",
    "                count += 1\n",
    "                res = max(res, count)\n",
    "            else: count = 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        mx = cnt = 0\n",
    "        for n in nums:\n",
    "            if n == mx:\n",
    "                cnt += 1\n",
    "                if cnt > ans:\n",
    "                    ans = cnt\n",
    "            elif n > mx:\n",
    "                mx = n\n",
    "                ans = cnt = 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx=ans=cnt=0\n",
    "        for num in nums:\n",
    "            if num > mx:\n",
    "                mx=num\n",
    "                ans=cnt=1\n",
    "            elif num==mx:\n",
    "                cnt+=1\n",
    "                ans=max(cnt,ans)\n",
    "            else:\n",
    "                cnt=0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)  # 找到数组中的最大值\n",
    "        ans = cnt = 0  # 初始化最长子数组的长度（ans）和当前计数（cnt）\n",
    "        \n",
    "        for x in nums:  # 遍历整个数组\n",
    "            if x == mx:  # 如果当前元素等于最大值\n",
    "                cnt += 1  # 增加计数\n",
    "                ans = max(ans, cnt)  # 更新最长子数组的长度\n",
    "            else:  # 如果当前元素不等于最大值\n",
    "                cnt = 0  # 重置计数\n",
    "                \n",
    "        return ans  # 返回最长子数组的长度\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        x = max(nums)\n",
    "        ans = 1\n",
    "        pre = nums[0]\n",
    "        cnt = 1\n",
    "        for num in nums[1:]:\n",
    "            if num == pre:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                if pre == x and cnt > ans:\n",
    "                    ans = cnt\n",
    "                pre = num\n",
    "                cnt = 1\n",
    "        if pre == x and cnt > ans:\n",
    "            ans = cnt\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        maxnum = max(nums)\n",
    "        n = len(nums)\n",
    "        result = 1\n",
    "        left, right = 0, 0\n",
    "        while(left < n):\n",
    "            if(nums[left] == maxnum):\n",
    "                right = left\n",
    "                while(right < n and nums[right] == maxnum):\n",
    "                    right += 1\n",
    "                result = max(result, right - left)\n",
    "                left = right \n",
    "            else:\n",
    "                left += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        maxx=max(nums)\n",
    "        ans=1\n",
    "        temp=0\n",
    "        for i in range(n):\n",
    "            if nums[i]!=maxx:\n",
    "                temp=0\n",
    "            else:\n",
    "                temp+=1\n",
    "                ans=max(ans,temp)\n",
    "        \n",
    "        \n",
    "        return ans\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        def func(nums):\n",
    "            n = len(nums)\n",
    "\n",
    "            maxnum = max(nums)\n",
    "\n",
    "            result = 0\n",
    "            tmp=0\n",
    "            for i in range(n):\n",
    "                if nums[i]==maxnum:\n",
    "                    tmp+=1\n",
    "                    result = max(result,tmp)\n",
    "                else:\n",
    "                    tmp=0\n",
    "\n",
    "            return result\n",
    "        \n",
    "        return func(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def longestSubarray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "\n",
    "        ma = 0\n",
    "        res = 0\n",
    "        cnt = 0\n",
    "        for num in nums:\n",
    "            if num == ma:\n",
    "                cnt += 1\n",
    "            elif num > ma:\n",
    "                ma = num\n",
    "                res = cnt = 1\n",
    "            else:\n",
    "                res = max(res, cnt)\n",
    "                cnt = 0\n",
    "        \n",
    "        return max(res, cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        target=max(nums)\n",
    "        ans=1\n",
    "        last=-1\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        while i<n:\n",
    "            if nums[i]==target:\n",
    "                for j in range(i,n):\n",
    "                    if nums[j]!=target:\n",
    "                        ans=max(ans,j-i)\n",
    "                        i=j\n",
    "                        break\n",
    "                else:\n",
    "                    ans=max(ans,n-i)\n",
    "                    return ans\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        m = max(nums)\n",
    "        ret = 1\n",
    "        cnt = 0\n",
    "        for x in nums:\n",
    "            if x == m:\n",
    "                cnt += 1\n",
    "                ret = max(ret, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = mx = cnt = 0\n",
    "        for x in nums:\n",
    "            if x > mx:\n",
    "                mx = x\n",
    "                ans = cnt = 1\n",
    "            elif x == mx:\n",
    "                cnt += 1\n",
    "                if cnt > ans:\n",
    "                    ans = cnt\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for num in nums:\n",
    "            if num == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # # 脑筋急转弯\n",
    "    # 两次遍历\n",
    "    def longestSubarray1(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "    # 一次遍历\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = mx = cnt = 0\n",
    "        for x in nums:\n",
    "            if x > mx:\n",
    "                mx = x\n",
    "                ans = cnt = 1\n",
    "            elif x == mx:\n",
    "                cnt += 1\n",
    "                if cnt > ans:\n",
    "                    ans = cnt\n",
    "            else:\n",
    "                cnt = 0\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\r\n",
    "        mx = max(nums)\r\n",
    "        res = 0\r\n",
    "        cnt = 0\r\n",
    "        for x in nums:\r\n",
    "            if x == mx:\r\n",
    "                cnt += 1\r\n",
    "                res = max(res, cnt)\r\n",
    "            else:\r\n",
    "                cnt = 0\r\n",
    "        return res\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        ans = mx = cnt = 0\n",
    "        for x in nums:\n",
    "            if x > mx:\n",
    "                mx = x\n",
    "                ans = cnt = 1\n",
    "            elif x == mx:\n",
    "                cnt += 1\n",
    "                if cnt > ans:\n",
    "                    ans = cnt\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        m = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for num in nums:\n",
    "            if num == m:\n",
    "                cnt += 1\n",
    "                ans = max(cnt, ans)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt = cnt+1\n",
    "                ans = max(ans,cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "                   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans, cur = 0, 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == mx:\n",
    "                cur += 1\n",
    "            else:\n",
    "                ans = max(ans, cur)\n",
    "                cur = 0\n",
    "        return max(ans, cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        last=0\n",
    "        ans=0\n",
    "        ans_count=0\n",
    "        count=0\n",
    "        for i in nums:\n",
    "            if i!=last:\n",
    "                if last>ans:\n",
    "                    ans = last\n",
    "                    ans_count = count\n",
    "                elif last==ans:\n",
    "                    ans_count=max(ans_count,count)\n",
    "                last=i\n",
    "                count=1\n",
    "            else:\n",
    "                count+=1\n",
    "        if last>ans:\n",
    "            ans = last\n",
    "            ans_count = count\n",
    "        elif last==ans:\n",
    "            ans_count=max(ans_count,count)\n",
    "        return ans_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        ans = cnt = 0\n",
    "        for x in nums:\n",
    "            if x == mx:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        maxnum = max(nums)\n",
    "        result = 1\n",
    "        nums.append(-inf)\n",
    "        start = -1\n",
    "        for i,num in enumerate(nums):\n",
    "            if start == -1 and num == maxnum:\n",
    "                start = i\n",
    "            if start != -1 and num != maxnum:\n",
    "                result = max(result,i-start)\n",
    "                start = -1\n",
    "            # print(start,result)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestSubarray(self, nums: List[int]) -> int:\n",
    "        m = max(nums)\n",
    "        index = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == m:\n",
    "                index.append(i)\n",
    "        p = index[0]\n",
    "        s = 1\n",
    "        l = [1]\n",
    "        for i in range(1, len(index)):\n",
    "            if (index[i] - p) == 1:\n",
    "                p = index[i]\n",
    "                s+=1\n",
    "            else:\n",
    "                p = index[i]\n",
    "                l.append(s)\n",
    "                s=1\n",
    "        l.append(s)\n",
    "        return max(l)\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
